const log4js = require('koa-log4');
const logger = log4js.getLogger('routes::staff');
const router = require('koa-router')();
exports = module.exports = router;
const mongodb = require('mongodb');
const ObjectId = mongodb.ObjectId;

const _ = require('lodash');
const util = require(global.prefixPath + '/commons/util');
const site_service = require(global.prefixPath + '/services/site');
const info_service = require(global.prefixPath + '/services/info');
var querystring = require('querystring');

router.get('/:city/search', async (ctx, next) => {
    // 推广部分
    let fields = {adsenses: 1, _id: 0};
    let adsenses = (await site_service.findById(ctx.site._id, fields)).adsenses;
    let allids = [];
    for (let key in adsenses) {
        allids = allids.concat(adsenses[key]);
    }
    allids = util.toObjectId(allids);

    let allads = await info_service.findByIds(allids);

    // 组合数据
    for (let key in adsenses) {
        let ads = _.filter(allads, (val) => {
            return _.indexOf(adsenses[key], val._id.toString()) >= 0;
        });
        adsenses[key] = ads;
    }
    ctx.state.adsenses = adsenses;
    // 推广 end

    let params = querystring.parse(ctx.req._parsedUrl.query);

    if (params.query) {
        console.log('list 输出 params.query =');
        console.log(params.query);
        let querysource = JSON.parse(params.query);
        querysource.site = ctx.site._id.toString();
        for (var key in querysource) {
            if (querysource[key]) {
                if (key == 'category' || key == 'estate.type') {
                    querysource[key] = {$in: querysource[key]}
                }
                if (key.indexOf('estate.transferFee') != -1) {
                    querysource[key] = {$lte: querysource[key]}
                }
                if (key.indexOf('estate.rental') != -1 || key.indexOf('estate.area') != -1) {
                    querysource[key] = {$gte: querysource[key][0], $lte: querysource[key][1]}
                }
            } else {
                delete querysource[key];
            }
        }
        console.log('list 输出 querysource =')
        console.log(querysource);
        console.log('----------- querysouurce -------------');
        let q = {
            query: querysource,
            sort: params.sort ? JSON.parse(params.sort) : {
                createAt: -1
            },
            page: params.page ? JSON.parse(params.page) : {
                index: 1,
                size: 10
            }
        }
        // let result = await city_service.findByPinyin(city);
        let info_result = await info_service.find(q);
        console.log('------------- info-result -----------');
        console.log(info_result);
        console.log('------------- info-result end -----------');
        ctx.state.pub_info = info_result;
        ctx.state.query_info = params.query;
        ctx.state.query_state = JSON.parse(params.query);

    }

    // ctx.state.search_info = result;
    return await ctx.render('search/index');
});
